#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initUI();
}

void MainWindow::initUI()
{
    // 设置背景色
    QPalette palette;
    palette.setColor(QPalette::Window, QColor(237,243,247));
    this->setPalette(palette);
    // 设置窗口为屏幕大小
    QRect screenRect = QApplication::desktop()->availableGeometry(QApplication::desktop()->primaryScreen());
    this->resize(screenRect.width(), screenRect.height());
    qDebug() << screenRect.width() << " " << screenRect.height();
    // 设置窗口标题
    this->setWindowTitle("费马点计算");
    this->setWindowIcon(QIcon(":/res/title.jpeg"));
    // 状态栏信息
    statusBar()->showMessage("version 1.0");

    //整体为水平布局
    QHBoxLayout *hLayout1 = new QHBoxLayout();
    hLayout1->setMargin(0);

    // 第一个控件，画图区域
    drawLabel = new DrawLabel();
    drawLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    connect(drawLabel, SIGNAL(signalClicked(int, double, double)), this, SLOT(slotChangeLabel(int, double, double)));
    hLayout1->addWidget(drawLabel);

    // 第二个控件，分割线
    QWidget *lline = new QWidget();
    lline->setMinimumWidth(5);
    lline->setStyleSheet(QString::fromUtf8("border:3px solid rgb(0, 250, 102)"));
    hLayout1->addWidget(lline);

    // 第三部分，整体看做一个垂直布局
    btnClear = new QPushButton("清除绘画区");
    connect(btnClear, SIGNAL(clicked()), this, SLOT(slotClearDraw()));

    labelA = new QLabel("A点坐标:\n");
    labelA->setAlignment(Qt::AlignCenter);
    labelB = new QLabel("B点坐标:\n");
    labelB->setAlignment(Qt::AlignCenter);
    labelC = new QLabel("C点坐标:\n");
    labelC->setAlignment(Qt::AlignCenter);
    labelA1 = new QLabel("A1点坐标:\n");
    labelA1->setAlignment(Qt::AlignCenter);
    labelB1 = new QLabel("B1点坐标:\n");
    labelB1->setAlignment(Qt::AlignCenter);
    labelC1 = new QLabel("C1点坐标:\n");
    labelC1->setAlignment(Qt::AlignCenter);
    labelDst = new QLabel("费马点坐标:\n");
    labelDst->setAlignment(Qt::AlignCenter);
    labelDesc = new QLabel("描述:\n");
    labelDesc->setAlignment(Qt::AlignCenter);
    // 自动换行
    labelDesc->setWordWrap(true);

    QVBoxLayout *vLayout1 = new QVBoxLayout();
    vLayout1->setMargin(0);
    QWidget *widget1 = new QWidget();
    widget1->setFixedWidth(200);
    //widget1->setStyleSheet(QString::fromUtf8("border:3px solid blue"));
    vLayout1->addWidget(btnClear);
    vLayout1->addWidget(labelA);
    vLayout1->addWidget(labelB);
    vLayout1->addWidget(labelC);
    vLayout1->addWidget(labelA1);
    vLayout1->addWidget(labelB1);
    vLayout1->addWidget(labelC1);
    vLayout1->addWidget(labelDst);
    vLayout1->addWidget(labelDesc);
    widget1->setLayout(vLayout1);
    hLayout1->addWidget(widget1);

    // 将水平布局应用到整体
    ui->centralwidget->setLayout(hLayout1);
    ui->centralwidget->setContentsMargins(0, 0, 0, 0);
}

void MainWindow::slotClearDraw()
{
    // 清空数据
    drawLabel->clearPoint();
    labelA->setText("A点坐标:\n");
    labelB->setText("B点坐标:\n");
    labelC->setText("C点坐标:\n");
    labelA1->setText("A1点坐标:\n");
    labelB1->setText("B1点坐标:\n");
    labelC1->setText("C1点坐标:\n");
    labelDst->setText("费马点坐标:\n");
    labelDesc->setText("描述:\n");
}

void MainWindow::slotChangeLabel(int type, double x, double y)
{
    qDebug() << "draw change";
    // Y坐标去反，因为QT的y轴向下是正
    if(type==0)
    {
        labelA->setText("A点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==1)
    {
        labelB->setText("B点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==2)
    {
        labelC->setText("C点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==3)
    {
        labelA1->setText("A1点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==4)
    {
        labelB1->setText("B1点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==5)
    {
        labelC1->setText("C1点坐标:\n" + QString::number(x) +"," + QString::number(-y));
    }
    else if(type==6 || type==7 || type==8 || type==9)
    {
        labelDst->setText("费马点坐标:\n" + QString::number(x) +"," + QString::number(-y));
       if(type==6)
       {
            labelDesc->setText("描述:\n" + QString("三角行的三个角都小于120度，费马点在内部"));
       }
       else
       {
            labelDesc->setText("描述:\n" + QString("三角形有一个角大于120度，费马点就是这个角对应的顶点"));
       }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

